$(function () {
    $(".scanPwd").click(function () {

        //隐藏 、 查看密码
        if (!$(this).hasClass("scan")) {

            $(this).addClass("scan")
                .attr("src", "assets/images/login/show.png")
                .prev("input")
                .attr("type", "text");

        } else {

            $(this).removeClass("scan")
                .attr("src", "assets/images/login/hide.png")
                .prev("input")
                .attr("type", "password");

        }

    });

    $(".btn").click(function () {

        loginin()

    });
	
	$(document).keypress(function(event) {
		let key = event.keyCode;
		if (key == '13') {
			loginin()
		}
	})
	
	function loginin(){
		if ($("#username").val() == '' || $("#username").val() == null) {
		    alert('请输入账号');
		    return false;
		}
		
		if ($("#password").val() == '' || $("#password").val() == null) {
		    alert('请输入密码');
		    return false;
		}
		
		$.get(login_URL + "/ords/cl/login/checkpwd", {
		    username: $("#username").val(),
		    pwd: $("#password").val()
		}, function (e) {
			localStorage.clear()
		    if (e.checkResult == '200') {
		        localStorage.setItem('user', $("#username").val());
		        localStorage.setItem('pwdmd5', e.pwdmd5);
				getPermission($("#username").val())
		        
		    } else {
		        alert(e.desc);
		        return false;
		    }
		
		});
	}
	
	
	function getPermission(name){
		$.get(
		  `${API_URL}getdata?username=${name}&chartPage=704&status=2`,
		  {},
		  function (res) {
			  console.log(res)
			  let re = eval("(" + res + ")");
			  localStorage.setItem('permission',JSON.stringify(re.items[0].charts[0].data[0]))
			  window.location.href = 'Statistics-2022.html'
		  })
	}
});